package com.tuanzhang.mall.goods.domain.service.impl;

import com.tuanzhang.mall.goods.domain.aggregate.Spu;
import com.tuanzhang.mall.goods.domain.service.SpuService;
import com.tuanzhang.mall.goods.infrastructure.repository.SpuRepository;
import com.tuanzhang.mall.seller.types.exception.ApiException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class SpuServiceImpl implements SpuService {

    private final SpuRepository spuRepository;

    public SpuServiceImpl(@Qualifier("mockSpuRepository") SpuRepository spuRepository) {
        this.spuRepository = spuRepository;
    }

    @Override
    public Spu recommend(Spu spu) {
       int count = spuRepository.getRecommentCount(spu.getSpuCode());
       if (count >= 10) {
           throw new ApiException("推荐商品数量不能超过10个");
       }

       spu.recommend();
       return spu;
    }
}
